package com.example.servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import com.example.dao.NovelDao;
import com.example.dao.UserDao;
import com.example.domain.Chapter;
import com.example.domain.Novel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
@WebServlet("/NovelContentPayServlet")
public class NovelContentPayServlet extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException{
        response.setContentType("text/html;charset=utf-8");
        Integer novel_id = Integer.parseInt(request.getParameter("novel_id"));
        Integer chapter_id = Integer.parseInt(request.getParameter("chapter_id"));
        NovelDao novelDao = new NovelDao();
        UserDao userDao = new UserDao();
        HttpSession session = request.getSession();
        session.setAttribute("user", userDao.getUserById(1));
        Chapter chapter = novelDao.getChapterById(chapter_id, novel_id);
        if (chapter == null){
            request.setAttribute("error", "章节不存在");
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }
        String content = chapter.getContent().replaceAll("\\s{1,}", "<br>").trim();
        chapter.setContent(content);
        request.setAttribute("chapter", chapter);
        request.setAttribute("novel", novelDao.getNovelById(novel_id));
        request.getRequestDispatcher("novelContent.jsp").forward(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException{
        doGet(request,response);
    }
}
